package com.pj.wisdomlogistics.ui.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.ServicePersonnel;
import com.pj.wisdomlogistics.mvp.contract.PoritionContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.PoritionPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.PortionAdapter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PoritionActivity extends MvpActivity<PoritionPresenter> implements PoritionContract.View, PortionAdapter.listener {
    @BindView(R.id.rv_portion)
    RecyclerView mRvPortion;

    private PortionAdapter mAdapter;

    private Map<String , String> map;
    private String ids = "";

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        mRvPortion.setLayoutManager(new GridLayoutManager(this , 2 , LinearLayoutManager.VERTICAL , false));
        mAdapter = new PortionAdapter(this);
        mRvPortion.setAdapter(mAdapter);

        mAdapter.setListener(this);
        map = new HashMap<>();

        mPresenter.findAllServer();
        showProgressDialog();

    }



    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_porition;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @OnClick({R.id.iv_back , R.id.tv_sure})
    public void onClick(View mView){
        switch (mView.getId()){
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_sure:
                for(String key : map.keySet()){
                    ids += map.get(key) + ",";
                }
                ids = ids.substring(0 , ids.length() - 1);
                EventBus.getDefault().post(new EventCenter<>(Cons.portion.PORTION_IDS , ids));
                ActivityManagerUtil.create().finishActivity();
                break;
        }
    }


    @Override
    public void itemClick(String id, int type) {
        if(type == 1){
            map.put(id , id);
        }else if(type == 0){
            map.remove(id);
        }
    }

    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccess(List<ServicePersonnel> mList) {
        dismissProgressDialog();
        mAdapter.setList(mList);
    }
}
